
package com.flashextended.apps.logomaker.io
{
	import com.adobe.serialization.json.JSON;
	import com.flashextended.apps.logomaker.singleton.AppControl;
	import com.flashextended.apps.logomaker.singleton.AppLocales;
	import com.flashextended.utils.graphics.windows.AlertInfoWindow;
	import com.flashextended.utils.server.assetloader.LoadExternalAsset;
	import com.flashextended.utils.server.filereference.FileReferenceLocale;
	import com.flashextended.utils.server.phphttpservice.PhpHttpService;
	
	import flash.display.Bitmap;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.graphics.codec.PNGEncoder;
	import mx.managers.PopUpManager;
	
	public class ServerImplementation implements IOCom
	{
		private var callBackFeed:Function;
		
	    [Embed(source="../assets/images/save_logo.png")]
	    private var SaveLogo:Class;
		
		public function ServerImplementation()
		{
		}
		
		/**
		 * returns the application's locales
		 */ 
		public function getLocales(callBack:Function):void
		{
			callBackFeed = callBack;
			serverLocalesResult(null);
			return;
			var getFeed:PhpHttpService = new PhpHttpService();
			getFeed.makeConnection(AppLocales.getInstance().mainDomain, null, serverLocalesResult);
		}
		private function serverLocalesResult(data:Object):void
		{
			AppLocales.getInstance().data = AppLocales.getInstance().buildLocales();
			callBackFeed();
		}
		
		/**
		 * @callBack receives Bitmap as an argument
		 */ 
		public function uploadImage(callBack:Function):void
		{
			var fRef:FileReferenceLocale = new FileReferenceLocale(new FileFilter("Images", "*.jpg;*.gif;*.png"));
			fRef.browseForDataLocally(callBack);
		}
		
		public function saveLogo():void
		{
			var bmp:Bitmap = null;
			try{
				bmp = AppControl.getInstance().workarea.getLogo();
				var pngEncoder:PNGEncoder = new PNGEncoder();
				var imageBytes:ByteArray = pngEncoder.encode(bmp.bitmapData);
				var fRef:FileReferenceLocale = new FileReferenceLocale();
				fRef.saveFile(imageBytes, "yourLogo.png");
			}catch(e:Error)
			{
				var infoAlert:AlertInfoWindow = new AlertInfoWindow(300, 160, null, AppLocales.getInstance().data.locales['alert']);
				infoAlert.removeCancelButton();
				PopUpManager.addPopUp(infoAlert, AppControl.getInstance().logoMaker.parent);
				PopUpManager.centerPopUp(infoAlert);
				infoAlert.infoText = AppLocales.getInstance().data.locales['errorPreview'];
				infoAlert.show();
			}
		}
		
		
		/**
		 * retrives the symbols references from the server
		 */
		 public function getSymbols(callBack:Function):void
		 {
		 	var getSymbols:PhpHttpService = new PhpHttpService();
		 	getSymbols.makeConnection(AppLocales.getInstance().data.settings["symbolsURL"], null, callBack);
		 }
		 
		 public function readSymbol(callBack:Function, fileName:String):void
		 {
		 	//var loadAsset:AssetURLLoader = new AssetURLLoader(new URLRequest(AppLocales.getInstance().data.settings["readSymbolURL"]+"?id="+fileName), callBack);
		 	var loadAsset:LoadExternalAsset = new LoadExternalAsset(AppLocales.getInstance().data.settings["readSymbolURL"]+"?id="+fileName, callBack);
		 	loadAsset.isBusyCursor = false;
		 	loadAsset.start();
		 }
		
		
		
	}
}